//
//  QFADBanner.m
//  UIScrollViewDelegate
//
//  Created by Honey on 15/1/27.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "HXADBanner.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"

@implementation HXADBanner
{
    UIPageControl *_pageControl;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        self.backgroundColor = [UIColor yellowColor];
    }
    return self;
}

- (void)setArrays:(NSArray *)arrays
{
    if (arrays != _arrays)
    {
        _arrays = arrays;
        UIScrollView *sc = [[UIScrollView alloc] init];
        sc.frame = CGRectMake(0, 0, 320, 140);
        sc.tag = 1;
        
        for (NSInteger i = 0; i < _arrays.count; ++i)
        {
            UIImageView *v = [[UIImageView alloc] init];
            v.tag = 1000 + i;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClick:)];
            [v addGestureRecognizer:tap];
            v.userInteractionEnabled = YES;
            
            v.frame = CGRectMake(i * 320 + 320, 0, 320, 140);
//            v.image = self.arrays[i];//要显示的图片
            [v sd_setImageWithURL:[NSURL URLWithString:_arrays[i]]];
            
            [sc addSubview:v];
        }
        //添加冗余的view
        UIImageView *first = [[UIImageView alloc] init];
        [first sd_setImageWithURL:[NSURL URLWithString:[self.arrays lastObject]]];
        first.frame = CGRectMake(0, 0, 320, 140);
        
        UIImageView *last = [[UIImageView alloc] init];
        [last sd_setImageWithURL:[NSURL URLWithString:[self.arrays firstObject]]];
        last.frame = CGRectMake(320 * (self.arrays.count + 1), 0, 320, 140);
        
        [sc addSubview:last];
        
        [self addSubview:sc];
        sc.contentOffset = CGPointMake(320, 0);
        [sc addSubview:first];
        sc.contentSize = CGSizeMake(320 * (_arrays.count + 2), 0);
        sc.pagingEnabled = YES;
        sc.delegate = self;
        
        //关闭弹簧效果，防止露馅
        sc.bounces = NO;
        
        
        UIView *panel = [[UIView alloc] init];
        panel.frame = CGRectMake(0, 150 - 35, 320, 30);
        panel.alpha = 1;
        
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.frame = CGRectMake(320 / 2 - 50, 0, 100, 30);
        //_pageControl.center = CGPointMake(self.frame.size.width / 2, )
        _pageControl.numberOfPages = _arrays.count;
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        [panel addSubview:_pageControl];
        panel.backgroundColor = [UIColor clearColor];
        [self addSubview:panel];
        
        //通过定时器实现定时滚动的效果
//        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(change) userInfo:nil repeats:YES];
//        
//        // 将定时器放在混合模式
//        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
//        [runLoop addTimer:timer forMode:NSRunLoopCommonModes];
    }
}

//定时自动切换到下一张
- (void)change
{
    UIScrollView *sc = (UIScrollView *)[self viewWithTag:1];
    
    CGPoint offset = sc.contentOffset;
    offset.x += 320;
    NSInteger page = (offset.x - 320) / 320;
    
    if (offset.x == 320 * (self.arrays.count + 1))
    {
        offset.x = 320;
        page = 0;
    }
    
    [sc setContentOffset:offset animated:YES];
    _pageControl.currentPage = page;
}
//scrollview停止加速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    //将用户引导到指定的位置
    //根据当前位置来判断是第一张还是最后一张
    CGPoint offset = scrollView.contentOffset;
    //根据当前的偏移计算出页数  页数 = offset.x / 每一页的宽度
    NSInteger page = (offset.x - 320) / 320;
    
    if (offset.x == 0) //第一张，冗余的图片
    {
        //NSLog(@"改变前: %f",offset.x);
        [scrollView setContentOffset:CGPointMake(320 * _arrays.count, 0) animated:NO];
        page = _arrays.count + 1;
       // NSLog(@"改变后: %f",scrollView.contentOffset.x / 320);
    }
    else if (offset.x == 320 * (self.arrays.count + 1))
    {
        //NSLog(@"改变前: %f",offset.x);
        [scrollView setContentOffset:CGPointMake(320, 0) animated:NO];
        page = 0;
        //NSLog(@"改变后: %f",scrollView.contentOffset.x / 320);

    }
    _pageControl.currentPage = page;
}

- (void)onClick:(UITapGestureRecognizer *)sender
{
    NSLog(@"%d",sender.view.tag);
    _banBlock(sender.view.tag);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
